'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform', 'modalSearch',
        'datetimepicker', 'datetimepickerCN', 'ajaxTab'], function($, validate) {
  
  return {
    
    init: function(data) {
      this.initPage();
      this.initEmployee();
      this.submit(data);
      this.initDispatchAdjust();
      this.changeDispatchAdjust();
    },
    
    initPage: function(){
    	
        $('.btn-remove').on('click', function(e){
            $('input').val('');
        });
        
        $(document).find('form').bt_validate();
        
        $('select[name="oilTypeId"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
        });
        
        $('select.select2').select2({
            theme: "classic",
            placeholder : $(this).data("placeholder"),
            minimumResultsForSearch : -1,
            allowClear : true,
        });
        
        $('input[name="otherFees"]').on('change', function() {
            
            $("input[name='otherFees']").each(function(){
              
              var inputValue = $(this).val().trim();
              
              if(parseFloat(inputValue) != 0) {
                $(this).next().show();
              } else {
                $(this).next().hide();
              }
              
            })
            
          });
        
    },
    
    initEmployee:function() {
        
        var choseEmployees = {
           url: function(){
                var url = window.__CTX__.root + '/employees/query';
                return url + "?companyIds=" + $("input[name='organizationId']").val();
             },
           tableColumns: ['员工代码', '员工名称'],
           tableValues: ['code', 'name'],
           displayValues: ['code', 'name'],
           values: ['id']
        }
        
        $('[data-target="#modal-search-eId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-fId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-gId"]').modalsearch(choseEmployees);
        $('[data-target="#modal-search-hId"]').modalsearch(choseEmployees);
        
        var options = {
          time: 1000,
          url: window.__CTX__.root + "/employees/query",
          queryName: "code",
          value: ["id"],
          display: ["code", "name"]
        }
      
        $('.eId-search').koalaSearch(options);
        $('.fId-search').koalaSearch(options);
        $('.gId-search').koalaSearch(options);
        $('.hId-search').koalaSearch(options);
    },
    
    submit : function(data) {
      
      var that = this;
      
      $('form').shiftform({
        
        beforeSubmit: function (el) {
          
        	var validate = true;
        	
          //2. validate driver, main_driver cannot be same as first_vice_driver
          var mainDriver = $('input[name="mainDriver"]').val().trim();
          var firstViceDriver = $('input[name="firstViceDriver"]').val().trim();
          
          if(mainDriver != null && mainDriver!= undefined && mainDriver!="" && mainDriver === firstViceDriver){
            $('input[name="firstViceDriver"]').show_err_tooltip('请不要重复选择驾驶员！');
            validate = false;
          }
          
          //3. validate full_load_number
          var actualLoadNumber = data.actualLoadNumber;
          var inputLoadNumber = $('input[name="fullLoadNumber"]').val().trim();
          if(parseInt(actualLoadNumber) > parseInt(inputLoadNumber)){
            $('input[name="fullLoadNumber"]').show_err_tooltip('实际装载数量已大于装载能力！');
            validate = false;
          }
          
          //4.validate other fee auditor
          var isSet = false;
          
          $("input[name='otherFees']").each(function(){
          	
          	var inputValue = $(this).val().trim();
          	
          	if(parseFloat(inputValue) != 0) {
          	  isSet = true;
          	  return false;
          	}
          	
          })
          
          // 5. validate 行驶公里数调整为0时，调整理由不可填写
          var adjustmentDistance = $("input[name='adjustmentDistance']").val(); 
          var distanceAdjustRemark = $("textarea[name='distanceAdjustRemark']").val();
          if (adjustmentDistance == 0 && distanceAdjustRemark !== '') {
            $('input[name="adjustmentDistance"]').show_err_tooltip('行驶公里数调整为0时，调整理由不可填写！');
            validateResult = false;
          }
          
          // 6. validate otherFeeAuditor
          var otherFeeAuditor = $("input[name='otherFeeAuditor']").val();
          if(isSet && !otherFeeAuditor){
            $('input[name="otherFeeAuditor"]').show_err_tooltip('请填写其他费用审核人！');
            validateResult = false;
          }
          
          if(validate === false) {
            return false;
          }
          
          var roadFee = $('input[name="roadFee"]').val();
          var roadFeeConfirm = true;
          if (roadFee == 0) {
            roadFeeConfirm = confirm("路桥费等于0, 确认继续吗？");
          }
          
          
          $('form').trigger("validate.ajax.submit");
          return $.bt_validate.result[$.bt_validate.form_id];
          
        }
      
      });
              
     },
     initDispatchAdjust : function() {
     	
     	$(document).ready(function(e){
     		
     		var adjustType = $("select[name='distanceAdjustType']").val();
     		if(adjustType === "无需调整"){
     			$("input[name='adjustmentDistance']").attr("readonly", true); 
     			$("textarea[name='distanceAdjustRemark']").attr("readonly", true); 
     		}
     		
     	})
     },
     
     changeDispatchAdjust : function(){
     	
     	$("select[name='distanceAdjustType']").change(function(){
     		
     		var adjustType = $("select[name='distanceAdjustType']").val();
     		
     		if(adjustType !== "无需调整"){
     			$("input[name='adjustmentDistance']").attr("readonly", false); 
     			$("textarea[name='distanceAdjustRemark']").attr("readonly", false); 
     		} else {
     			//$("input[name='adjustmentDistance']").val('0.00');
     			$("input[name='adjustmentDistance']").attr("readonly", true);
     			$("textarea[name='distanceAdjustRemark']").val("");
     			$("textarea[name='distanceAdjustRemark']").attr("readonly", true);
     		}
     		
     	});
     	
     },
  }
})